<?php
/**
 * 前台详情页控制器。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace App\Http\Controllers;

use View;
use Cache;
use Config;
use Redirect;
use Response;
use App\Http\Model\Album;
use App\Http\Model\Detail;

class DetailController extends Shadow\ShadowController
{
    /**
     * 是否获取猜你喜欢模块内容。
     */
    const GET_LOVE = true;

    public function onGet($id)
    {
        $this->results['id'] = $id;
        $this->results['token'] = csrf_token();
        $a_results = $this->getAloneAlbum($id);
        if (!$a_results['album'] || $a_results['album']['status'] != Album::SHOW) {

            return Redirect::to('/');
        }
        $this->results['album'] = $a_results['album'];
        $this->results['lists'] = $a_results['lists'];

        return View::make('detail', $this->results);
    }

    public function onPost($id)
    {
        $i_code = 403;
        $a_result = array(
            'reason' => '浏览次数生成失败！'
        );
        $o_album = Album::find($id);
        if ($o_album) {
            $o_album->visit_times = $o_album->visit_times + 1;
            $o_album->save();
            $a_result['reason'] = '访问成功！';

            return $this->ajaxSuccess($a_result);
        }

        return $this->ajaxError($a_result);
    }

    /**
     * 获取详情页数据。
     * @param  int  $id 专辑编号
     * @return array
     */
    private function getAloneAlbum($id)
    {
        $a_results = array();
        $s_key = 'album_results' . $id . date('Y-m', time());
        if (Config::get('app.debug') == false && Cache::has($s_key)) {
            $a_results = json_decode(Cache::get($s_key), true);
            if ($a_results) {
                shuffle($a_results['lists']);
                return $a_results;
            }
        }
        $o_album = Album::find($id);
        if (!$o_album) {
            return $a_results;
        }
        $o_album->type->title;
        $o_tags = $o_album->tag;
        foreach ($o_tags as $tag) {
            $tag->gttitle->title;
        }
        $a_album = $o_album->toArray();
        unset($a_album['uid']);
        unset($a_album['type_id']);
        $o_lists = Detail::where('status', '=', 'show')
            ->where('pa_id', '=', $id)->get();
        $a_crossranges = $a_endwises = array();
        $a_lists = array();
        if (count($o_lists)) {
            foreach ($o_lists as $vv) {
                unset($vv['create_time']);
                unset($vv['status']);
                if (!Config::get('app.env') == 'production') {
                    continue;
                }
                $s_small = '-smallee';
                $s_high = '-highee';
                if ($vv->photo_type == 'crossrange') {
                    $s_small = '-smallheng';
                    $s_hign = '-highheng';
                }
                $vv['small_resource'] = $this->getBaseUrl($vv['resource'] . $s_small);
                $vv['high_resource'] = $this->getBaseUrl($vv['resource'] . $s_high);
                $vv['little_resource'] = $this->getBaseUrl($vv['resource'] . '-little');
                unset($vv['resource']);
                $a_lists[] = $vv->toArray();
            }
        }
        shuffle($a_lists);
        $a_results = array(
            'album' => $a_album,
            'lists' => $a_lists
        );
        Cache::put($s_key, json_encode($a_results), static::CACHE_LIFETIME);

        return $a_results;
    }
}
